﻿// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/AntdUI/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form11 : Form
    {
        public Form11()
        {
            InitializeComponent();

            listView1.View = View.Details;
            listView1.Columns.Add("产品", 150);
            listView1.Columns.Add("库存数量", 100);
            listView1.Columns.Add("状态", 100);

            // 添加示例数据
            AddItemWithColor("笔记本电脑", "50", "正常");
            AddItemWithColor("鼠标", "10", "低库存");
            AddItemWithColor("键盘", "3", "紧急");
            AddItemWithColor("显示器", "20", "正常");
        }

        private void AddItemWithColor(string product, string stock, string status)
        {
            var item = new ListViewItem(product);
            item.SubItems.Add(stock);
            item.SubItems.Add(status);

            // 根据库存数量设置颜色
            if (int.TryParse(stock, out int stockNum))
            {
                // 库存低于5时标红
                if (stockNum <= 5)
                {
                    item.SubItems[1].ForeColor = Color.White;
                    item.SubItems[1].BackColor = Color.Red;
                    item.SubItems[2].ForeColor = Color.Red;
                }
                // 库存低于15时标黄
                else if (stockNum <= 15)
                {
                    item.SubItems[1].ForeColor = Color.DarkRed;
                    item.SubItems[1].BackColor = Color.Yellow;
                    item.SubItems[2].ForeColor = Color.Orange;
                }
            }

            listView1.Items.Add(item);
        }
        // 双击子项时改变颜色（交互示例）
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var hitTest = listView1.HitTest(e.X, e.Y);
            if (hitTest.SubItem != null)
            {
                // 切换子项颜色
                //hitTest.SubItem.ForeColor = hitTest.SubItem.ForeColor == Color.Blue
                //    ? Color.Black
                //    : Color.Blue;
                hitTest.SubItem.ForeColor = Color.Red;
            }
        }
    }
}
